# coding=utf8

from app import create_app
from app.modules.common.jsonFileUtil import getJsonFile
from app.extensions import db
from app.modules.common.utils import DbCommit
from app.modules.entrance.models import EntranceInfo
from app.modules.apps.models import AppInfo

application = create_app(flask_config = 'local')


def main(_type, _env_name, _zone_code):
    with application.app_context():
        db.session.add(EntranceInfo(type = _type, env_name = _env_name, zone_code = _zone_code, prefix = _type,
                                    expect_status = "GNONE", current_status = "GNONE"))
        DbCommit()


def importUschedule():
    with application.app_context():
        for _app in AppInfo.query.filter_by(app_name = "uschedule", env_name = "online", group = "A").all():
            _info = EntranceInfo(type = "uschedule", env_name = _app.env_name, zone_code = _app.zone_code,
                                 hosts = "%s:8016" % _app.host, prefix = "uschedule",
                                 suffix = "/uschedule/job.do?cmd=open$resetBlackList", expect_status = "GNONE",
                                 current_status = "GNONE")
            print _info.to_dict()
            db.session.add(_info)
            pass
        DbCommit()
        pass


def importHa(_env_name, _ip):
    with application.app_context():
        _info = EntranceInfo(type = "haproxy", env_name = _env_name, zone_code = "uc.gzidc.area",
                             hosts = "%s:7777" % _ip, prefix = "haproxy", suffix = "/api/v1/haproxy/barrier",
                             expect_status = "GNONE", current_status = "GNONE")
        print _info.to_dict()
        db.session.add(_info)
        DbCommit()
        pass


def importElb(_env_name, _zone_code, _hosts, _dns, _gray_dns, _app_name):
    with application.app_context():
        _info = EntranceInfo(type = "elb", env_name = _env_name, zone_code = _zone_code, hosts = _hosts,
                             user = "AKIAIHJWZTHVJKJVTZKQ:CJj86PxIAR3IXeJ8/ha1Dp87HwKJDwuWxS7tgeD3",
                             prefix = "elb", suffix = "?dns=%s&gray-dns=%s&type=%s" % (_dns, _gray_dns, _app_name),
                             expect_status = "GNONE", current_status = "GNONE")
        print _info.to_dict()
        db.session.add(_info)
        DbCommit()
        pass


if __name__ == '__main__':
    _env_name = "online"

    _types = ["jmx", "hosts"]
    _zone_codes = ["uc.gzidc.area", "uc.usaws.area", "uc.euaws.area", "uc.auaws.area"]

    _has = ['10.32.2.33', '10.32.2.34', '10.32.2.35', '10.32.2.36', '10.32.0.248']

    # importUschedule()
    _zone_code = "uc.usaws.area"
    _hosts = "us-west-1/ustom2"
    _dns = "ustom2-1914634820.us-west-1.elb.amazonaws.com"
    _gray_dns = "ustom2-gray-664924514.us-west-1.elb.amazonaws.com"
    _app_name = "tom"
    importElb(_env_name, _zone_code, _hosts, _dns, _gray_dns, _app_name)

    # for _ip in _has:
    #     importHa(_env_name, _ip)
    #     pass

    # for _type in _types:
    #     for _zone_code in _zone_codes:
    #         main(_type, _env_name, _zone_code)
    #     pass
    # pass
